package com.smart.common.entities.core;


import cn.hutool.core.collection.CollectionUtil;
import io.ebean.PagedList;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.log4j.Log4j2;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * 返回分页对象
 *
 * @author 夏绪秋
 * @date 2022-08-08
 */
@Data
@Log4j2
@NoArgsConstructor
@AllArgsConstructor
@ApiModel
public class Page<T> implements Serializable {

    private static final long serialVersionUID = 8545996863226528798L;
    @ApiModelProperty("分页总页数")
    protected int total;
    @ApiModelProperty("分页条数")
    protected int pageSize;
    @ApiModelProperty("当前页码")
    protected int pageNo;
    @ApiModelProperty("返回记录明细")
    protected List<T> records;

    @ApiModelProperty("是否存下一页")
    protected boolean hasNext;

    @ApiModelProperty("是否存上一页")
    protected boolean hasPrev;


    public Page<T> load(PagedList<T> pagedList) {

        this.total = pagedList.getTotalCount();
        this.pageNo = pagedList.getPageIndex() + 1;
        this.hasNext = pagedList.hasNext();
        this.pageSize = pagedList.getPageSize();
        List<T> list = pagedList.getList();


        this.records = this.total <= 0 ? Collections.emptyList() : CollectionUtil.isNotEmpty( list ) ? list : Collections.emptyList();
        return this;
    }


}